package com.tank.entrity;

import com.tank.control.DirectionEnum;
import com.tank.model.GameModel;

import java.awt.*;
import java.util.UUID;

import static com.tank.window.GameApplicationWindows.WINDOWS_HEIGHT;
import static com.tank.window.GameApplicationWindows.WINDOWS_WIDTH;

public class Bullet extends GameObject {
    /** 子弹的宽度 */
    public final static int BULLET_WIDTH = 10;
    /** 子弹的高度 */
    public final static int BULLET_HEIGHT = 10;
    /** 子弹的速度 */
    private final static double BULLET_SPEED = 1.0;
    /** 子弹的方向 */
    private final DirectionEnum directionEnum;
    /** 和坦克的类型一致 */
    private final GroupType groupType;
    /** 碰撞检测使用的矩形 */
    private final Rectangle bulletRectangle;
    /** 子弹是否可用 */
    private boolean isEnabled = true;
    /** 子弹的颜色跟随坦克 */
    private Color bulletColor = Color.BLACK;

    private UUID id = UUID.randomUUID();

    private UUID playerId;

    public Bullet(UUID playerId, int x, int y, DirectionEnum dir, GroupType group) {
        this.playerId = playerId;
        this.x = x;
        this.y = y;
        this.directionEnum = dir;
        this.groupType = group;
        bulletRectangle = new Rectangle();
        bulletRectangle.x = (int) this.x;
        bulletRectangle.y = (int) this.y;
        bulletRectangle.width = BULLET_WIDTH;
        bulletRectangle.height = BULLET_HEIGHT;

    }

    public Bullet(int x, int y, DirectionEnum directionEnum, GroupType groupType) {
        this.x = x;
        this.y = y;
        this.directionEnum = directionEnum;
        this.groupType = groupType;
        this.bulletRectangle = new Rectangle((int) this.x, (int) this.y, BULLET_WIDTH, BULLET_HEIGHT);
    }

    public UUID getId() {
        return id;
    }

    public UUID getPlayerId() {
        return playerId;
    }

    public GroupType getGroupType() {
        return groupType;
    }

    public DirectionEnum getDirectionEnum() {
        return directionEnum;
    }

    public void paint(Graphics graphics) {
        graphics.setColor(bulletColor);
        graphics.fillOval((int) x, (int) y, BULLET_WIDTH, BULLET_HEIGHT);
        Tank.BARREL.release();
        if (GameModel.getInstance() != null && !isEnabled) {
            GameModel.getInstance().removeObject(this);
        }
        move();
    }

    @Override
    public int getWidth() {
        return BULLET_WIDTH;
    }

    @Override
    public int getHeight() {
        return BULLET_HEIGHT;
    }

    public void move() {
        switch (directionEnum) {
            case LEFT:
                x -= BULLET_SPEED;
                break;
            case RIGHT:
                x += BULLET_SPEED;
                break;
            case UP:
                y -= BULLET_SPEED;
                break;
            case DOWN:
                y += BULLET_SPEED;
                break;
        }
        this.bulletRectangle.x = (int) this.x;
        this.bulletRectangle.y = (int) this.y;
        if (x < 0 | y < 0 | x > WINDOWS_WIDTH | y > WINDOWS_HEIGHT) {
            this.isEnabled = false;
        }
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public Rectangle getBulletRectangle() {
        return bulletRectangle;
    }

    public void destroy() {
        this.isEnabled = false;
    }

    public void setId(UUID id) {
        this.id = id;
    }
}
